﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TRTLOOM
    {

        public TRTLOOM()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VAMSTAMM();
            this.DataContext = MV;
            InitializeComponent();
            ART = app.TERMNO.PadRight(8);
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            base.Window_Loaded(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
        }
        

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                switch(app.company)
                {
                    default:
                        SResult = TMV.QHRRLOOM();
                        break;
                }
                IsBusy = true;
                dwData = TBMain.NewRow();
                TMV.SetUpNewRow(ref dwData);
                dwData["AMTYP"] = "WEBMA   ";
                TMV.DAMTYP(dwData);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwData["AMNRKRS"] = "M ";
                    dwData["AMLFDNR"] =Helpers.Helper.Left( row["AMLFDNR"].ToString().PadRight(4),4);
                    dwData["AMPOS"] = "00";
                    dwData["AMBDEMA"] = Helpers.Helper.Left(row["AMLFDNR"].ToString().PadRight(8), 8);
                    
                    dwData["AMLO"] = Helpers.Helper.Left(row["AMLO"].ToString().PadRight(2), 2);
                    dwData["AMLB"] = Helpers.Helper.Left(row["AMLO"].ToString().PadRight(2), 2);
                    dwData["AMLPLATZ"] = Helpers.Helper.Left(row["AMLB"].ToString().PadRight(6), 6);
                    dwData["AMSTKNR"] = Helpers.Helper.Left(Helpers.Helper.ConvertGB2312To8859P1( row["AMSTKNR"].ToString()).PadRight(12), 12);
                    dwData["AMTSTKNR"] = Helpers.Helper.Left(Helpers.Helper.ConvertGB2312To8859P1(row["AMTSTKNR"].ToString()).PadRight(12), 12); 
                    TMV.InserAMSTAMM(dwData);
                }
                base.QueryCommand(sender, e);
                IsBusy = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
    }
    public partial class VAMSTAMM
    {
        SqlConnection _SqlServerDB;
        SqlConnection SqlServerDB
        {
            get
            {
                if (_SqlServerDB == null)
                    _SqlServerDB = new SqlConnection(app.SqlDB);
                return _SqlServerDB;
            }
        }
        public DataSet QHRRLOOM()
        {
            SqlCommand SQLCmd;
            
            SQLCmd = SqlServerDB.CreateCommand();
            SQLCmd.CommandText = "select MachineName as AMLFDNR,MC.DeptID As AMLO,MC.GroupID5 AS GroupID5,MC.GroupID6 AS AMLB, DeptName as AMSTKNR,GroupName0 as AMTSTKNR from Machine  MC "
                + " inner join tDept DP on MC.DeptID=DP.DeptID "
                + " inner join tcGroupName GP on MC.GroupID6 =GP.GroupID "
                + " order by 1";
            SResult = INTEXDBC.Connect2Proxy(SQLCmd);
            SResult.Tables[0].TableName = TableName; 
            return SResult;
        }
    }
}
